package main

import (
	"context"

	"github.com/go-kit/log"
	"github.com/go-kit/log/level"
)

type Customer struct {
	Customerid string `json:"customerid"`
	Email      string `json:"email"`
	Phone      string `json:"phone"`
}

type AccountService interface {
	CreateCustomer(ctx context.Context, customer Customer) (string, error)
	GetCustomerById(ctx context.Context, id string) (interface{}, error)
	GetAllCustomers(ctx context.Context) (interface{}, error)
	UpdateCustomer(ctx context.Context, customer Customer) (string, error)
	DeleteCustomer(ctx context.Context, id string) (string, error)
}

type accountservice struct {
	repository Repository
	logger     log.Logger
}

func NewService(repo Repository, logger log.Logger) AccountService {
	return &accountservice{
		repository: repo,
		logger:     logger,
	}
}

func (s accountservice) CreateCustomer(ctx context.Context, customer Customer) (string, error) {
	logger := log.With(s.logger, "method", "create")
	var msg = "success"
	if err := s.repository.CreateCustomer(ctx, customer); err != nil {
		level.Error(logger).Log("err from repo is ", err)
		return "", err
	}
	return msg, nil
}

func (s accountservice) GetCustomerById(ctx context.Context, id string) (interface{}, error) {
	logger := log.With(s.logger, "method", "GetCustomerById")

	var customer interface{}
	var empty interface{}
	customer, err := s.repository.GetCustomerById(ctx, id)
	if err != nil {
		level.Error(logger).Log("err", err)
		return empty, err
	}
	return customer, nil
}

func (s accountservice) GetAllCustomers(ctx context.Context) (interface{}, error) {
	logger := log.With(s.logger, "method", "GetAllCustomers")

	var customers interface{}
	var empty interface{}

	customers, err := s.repository.GetAllCustomers(ctx)
	if err != nil {
		level.Error(logger).Log("err", err)
		return empty, err
	}
	return customers, nil
}

func (s accountservice) DeleteCustomer(ctx context.Context, id string) (string, error) {
	logger := log.With(s.logger, "Method", "DeleteCustomer")
	msg, err := s.repository.DeleteCustomer(ctx, id)
	if err != nil {
		level.Error(logger).Log("err", err)
		return "", err
	}
	return msg, nil
}

func (s accountservice) UpdateCustomer(ctx context.Context, customer Customer) (string, error) {
	logger := log.With(s.logger, "Method", "UpdateCustomer")
	var msg = "success"
	msg, err := s.repository.UpdateCustomer(ctx, customer)
	if err != nil {
		level.Error(logger).Log("err from repo is ", err)
		return "", err
	}
	return msg, nil
}
